package tests;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

import java.util.List;

import mailsystem.Mailbox;
import mailsystem.Message;
import mailsystem.persistance.MailsystemPersistance;
import mailsystem.persistance.database.MailBoxDB;
import mailsystem.persistance.database.MailsystemPersistanceDB;

import org.junit.Before;
import org.junit.Test;

public class MailsystemPersistanceDBTests {

	
	MailBoxDB boxes;
	MailsystemPersistanceDB system;
	MailsystemPersistance persistanceMock = mock(MailsystemPersistance.class);
	int lenght;
	
	Mailbox mailBox = new Mailbox(111, "", "", "", persistanceMock);
	Mailbox mailBox2 = new Mailbox(222, "", "", "", persistanceMock);
	Message message = new Message("hola ariel",1, false);
	

	
	@Before
	public void doBefore()
	{
		boxes = new MailBoxDB(persistanceMock);
		boxes.changeToTestMode();
		system = new MailsystemPersistanceDB(3);
		system.changeToTestMode();	
	}
	

	@Test
	public void testAddMailbox() {
		
		int before = system.listAllMailbox().size();
		system.addMailbox(mailBox);
		int after = system.listAllMailbox().size();
		assertEquals(before + 1, after);	
		system.deleteMailbox(mailBox);

	}

	@Test
	public void testDeleteMailbox() {
		
		int before = system.listAllMailbox().size();
		system.addMailbox(mailBox);
		system.deleteMailbox(mailBox);	
		int after = system.listAllMailbox().size();
		assertEquals(before, after);
		
		
	}

	@Test
	public void testUpdateMailbox() {
		
		system.addMailbox(mailBox);		
		mailBox.setGreeting(mailBox.getGreeting() + message.getText());	
		system.updateMailbox(mailBox);
		List<Mailbox> boxes = system.listAllMailbox();		
		assertEquals(mailBox, boxes.get(boxes.size()-1));	
		system.deleteMailbox(mailBox);
		
	}

	@Test
	public void testListAllMailbox() {
		int before = system.listAllMailbox().size();		
		system.addMailbox(mailBox);		
		int after = system.listAllMailbox().size();	
		assertEquals(before + 1, after);	
		system.deleteMailbox(mailBox);
	}

	@Test
	public void testAddContact() {
		
		
		system.addMailbox(mailBox);
		system.addMailbox(mailBox2);		
		system.addContact(mailBox, mailBox2);	
		assertEquals(system.getAllContacts(mailBox).size(),1);		
		system.deleteContact(mailBox, mailBox2);
		system.deleteMailbox(mailBox);
		system.deleteMailbox(mailBox2);		
	}

	@Test
	public void testDeleteContact() {
		

		system.addMailbox(mailBox);
		system.addMailbox(mailBox2);		
		system.addContact(mailBox, mailBox2);
		system.deleteContact(mailBox, mailBox2);		
		assertEquals(system.getAllContacts(mailBox).size(),0);		
		system.deleteMailbox(mailBox);
		system.deleteMailbox(mailBox2);		
		
	}

	@Test
	public void testGetAllContacts() {

		system.addMailbox(mailBox);
		system.addMailbox(mailBox2);	
		system.addContact(mailBox, mailBox2);		
		assertEquals(system.getAllContacts(mailBox).size(),1);
		system.deleteContact(mailBox, mailBox2);		
		system.deleteMailbox(mailBox);
		system.deleteMailbox(mailBox2);
	}

	@Test
	public void testAddNewMessage() {

		system.addMailbox(mailBox);	
		system.addNewMessage(mailBox, message);		
		assertEquals(system.listAllNewMessages(mailBox).size(),1);
		system.deleteNewMessage(message);
		system.deleteMailbox(mailBox);
		
		
		
	}

	@Test
	public void testDeleteNewMessage() {

		system.addMailbox(mailBox);	
		system.addNewMessage(mailBox, message);
		system.deleteNewMessage(message);		
		assertEquals(system.listAllNewMessages(mailBox).size(),0);		
		system.deleteMailbox(mailBox);

	}

	@Test
	public void testListAllNewMessages() {

		system.addMailbox(mailBox);	
		system.addNewMessage(mailBox, message);		
		assertEquals(system.listAllNewMessages(mailBox).size(),1);
		system.deleteNewMessage(message);
		system.deleteMailbox(mailBox);
	}

	@Test
	public void testAddKeptMessage() {

		system.addMailbox(mailBox);
		system.addKeptMessage(mailBox, message);	
		assertEquals(system.listAllKeptMessages(mailBox).size(),1);
		system.deleteKeptMessage(message);
		system.deleteMailbox(mailBox);
	}

	@Test
	public void testDeleteKeptMessage() {

		system.addMailbox(mailBox);
		system.addKeptMessage(mailBox, message);
		system.deleteKeptMessage(message);	
		assertEquals(system.listAllKeptMessages(mailBox).size(),0);		
		system.deleteMailbox(mailBox);
	}

	@Test
	public void testListAllKeptMessages() {

		system.addMailbox(mailBox);
		system.addKeptMessage(mailBox, message);		
		assertEquals(system.listAllKeptMessages(mailBox).size(),1);
		system.deleteKeptMessage(message);
		system.deleteMailbox(mailBox);
	}

}
